/*
 * Copyright (c) 2025, 廖树财, <study_2030@163.com>
 *
 * PHS2.0-OTA is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2     
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
#pragma once
#include "iniparser.h"
#include "zlog.h"
#include "JsonParser.h"
#include "iotaimp.h"
#include "cQueue.h"
#include "common.h"
#include "BussinessTerminalManage.h"
// 进程诊断
typedef struct stru_proc_diagnostics
{
	int pid;
	char pname[128];
	char status[32];
	int anomaly; // 异常标志
	int cpuUsageMax;
	int cpuUsageMin;
	double cpuUsageAvg;
	int cpuUsageSum;
	int memUsageMax;
	int memUsageMin;
	double memUsageAvg;
	int memUsageSum;
	int count;
} ST_PROC_DIAGNOSTICS;

// 系统各项性能数据
typedef struct stru_system_Indicators
{
	int cpuTemp;  // cpu温度
	int cpuUsage; // cpu使用率
	int memUsage;
	float diskUsage;
} ST_SYS_INDICATORS;

typedef struct stru_disk_Indicators
{
	float diskUsage;
	char partition[128];
	int indication;
} ST_DISK_INDICATORS;

// 命令行上下文
typedef struct stru_cmd_context
{
	char pwd[64];
	time_t sessionTime; // sessionId两次命令不超过5分钟
} ST_CMD_CONTEXT;

// 网络测试上下文
typedef struct stru_net_context
{
	char mid[128];
	double delay[64];
	int send_num; // 发送包次数
	int recv_num; // 接收包次数
	int curr_num; // 当前包序列
	// time_t sessionTime; // sessionId两次命令不超过5分钟
} ST_NET_CONTEXT;

// 告警规则
typedef struct stru_SECURITY_RULE
{
	char ruleId[64];	  // 规则ID:每条规则需要有个id，便于后边更新或者删除
	char action[32];	  // 动作:ADD 、 DEL
	char indicator[32];	  // 指标类型：cpuTemp、cpuUsage、
	char ruleValueRemark[64]; //具体的磁盘分区，当indicator为diskUsage时必填
	char severity[32];	  // 告警级别:Critical紧急、Major重要、Minor次要、Warning提示
	int threshold;		  // 阈值
	char operator[8];	  // 判断字符：判断操作符EQ("="),NE("<>"),GT(">"),GE(">="),LT("<"),LE("<=")
	char unit[8];		  // 单位 °C
	int duration;		  // 统计周期 单位秒
	int count;			  // 统计次数
	time_t lastCountTime; // 上次统计时间
	int currCount;		  // 当前统计次数
	int isAlarm;
	pthread_mutex_t lock;
	int ruleType;         //标识位 1为默认配置，2为用户修改的
	int durationFlag;	  // 周期内是否已报警： 0未告警 1已告警  
} ST_SECURITY_RULE;

// 网络延时信息
typedef struct stru_NETINFO
{
	int ttl;	   // 当前跳数
	float lossPct; // 丢包率
	int sent;	   // 发送次数
	int recv;	   // 接收次数
	int last;	   // 最近一次时延
	int avg;	   // 评价时延
	int best;	   // 最佳时延
	int worst;	   // 最差时延
	float stddev;  // 抖动
	int anomaly;   // 异常点，•1：单跳的平均时延超过1000ms；•2：单跳的时延波动率超过50%；•3：单跳的丢包率超过50%；•0：表示正常
	int duration;  // 测试周期
} ST_NETINFO;

void readAlarmRule(dictionary *ini);
void *alarm_thread(void *param);
void security_proc(char *pcJson);
void sys_log_publish_x86();
void sys_log_publish();
void sys_log_publish_jsl();
void *netTest_thread(void *param);
int get_net_info(char *mid, ST_NETINFO *netInfo, char *model);
int mqtt_send_net_test(char *mid, int test_num, char *model);
void *net_Info_loop_thread(void *param);
